/**
 * @author: akolonitsky
 * @date: 12/8/11 12:34 PM
 */
package view.screens
{
    import model.LogProxy;
    import model.UserProxy;
    import model.vo.LogRecord;
    import model.vo.LogUpdateToken;
    import model.vo.UserVO;

    import mx.collections.ArrayCollection;

    import org.kolonitsky.alexey.debug.DebugWrapper;
    import org.kolonitsky.alexey.mvcext.NotificationMap;
    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.mediator.Mediator;

    import view.events.RecordEvent;

    public class LogMediator extends Mediator
    {
        public static const NAME:String = "logMediator";

        public function LogMediator(screen:LogScreen)
        {
            super(NAME, screen);
            DebugWrapper.trace("INFO: " + getMediatorName() + " created.");
        }

        public function get screen():LogScreen
        {
            return viewComponent as LogScreen;
        }




        //-------------------------------------------------------------------------
        //
        // Mediator overriden methods
        //
        //-------------------------------------------------------------------------

        override public function onRegister():void
        {
            DebugWrapper.trace("INFO: " + getMediatorName() + " registred.");
            sendNotification(Config.LOG_GET, "1m"); //TODO: Get first value from UI form.
            sendNotification(AppFacade.USER_AUTHORIZATION);

            screen.addEventListener(RecordEvent.TWEET, screen_tweetHandler);
            screen.addEventListener(RecordEvent.RECORD_DELETE, deleteRecordHandler);
            screen.addEventListener(RecordEvent.RECORD_CHANGE, changeRecordHandler);
            screen.addEventListener(RecordEvent.LOAD, loadRecordHandler);
        }

        override public function onRemove():void
        {
            DebugWrapper.trace("INFO: " + getMediatorName() + " removed.");
            screen.removeEventListener(RecordEvent.TWEET, screen_tweetHandler);
            screen.removeEventListener(RecordEvent.RECORD_DELETE, deleteRecordHandler);
            screen.removeEventListener(RecordEvent.RECORD_CHANGE, changeRecordHandler);
            screen.removeEventListener(RecordEvent.LOAD, loadRecordHandler);
        }

        override public function listNotificationInterests():Array
        {
            return [
                LogProxy.UPDATE,
                LogProxy.UPDATE_BALANCE,
                LogProxy.LOG_IS_EMPTY,
                LogProxy.NOT_ENOUGH_RECORDS,
                UserProxy.USER_AUTHORIZATION
            ]
        }

        override public function handleNotification(notification:INotification):void
        {
            switch (notification.getName())
            {
                case LogProxy.UPDATE:
                {
                    var token:LogUpdateToken = notification.getBody() as LogUpdateToken;
                    updateLog(token);
                    break;
                }

                case LogProxy.UPDATE_BALANCE:
                {
                    screen.balance = notification.getBody();
                    sendNotification(NotificationMap.PAGE_READY);
                    break;
                }

                case LogProxy.LOG_IS_EMPTY:
                {
                    sendNotification(NotificationMap.PAGE_READY);
                    break;
                }

                case LogProxy.NOT_ENOUGH_RECORDS:
                {
                    screen.totalRecordCount =  uint(notification.getBody());
                    break;
                }

                case UserProxy.USER_AUTHORIZATION:
                {
                    var user:UserVO = notification.getBody() as UserVO;
                    if (user)
                        screen.user = user;
                    break;
                }
            }
        }




        //-------------------------------------------------------------------------
        //
        // Private logic
        //
        //-------------------------------------------------------------------------

        private function updateLog(token:LogUpdateToken):void
        {
            switch (token.type)
            {
                case LogUpdateToken.LOAD:
                {
                    sendNotification(AppFacade.COLLECT_USER_STATISTIC);
                    sendNotification(Config.CURRENCIES_GET);

                    screen.records = new ArrayCollection(token.list);
                    break;
                }

                case LogUpdateToken.ADD:
                {
                    if (screen.records.length == 0)
                    {
                        sendNotification(AppFacade.COLLECT_USER_STATISTIC);
                        sendNotification(Config.CURRENCIES_GET);
                    }
                    screen.records.addItemAt(token.item, token.index);
                    screen.scrollDown();
                    break;
                }

                case LogUpdateToken.DELETE:
                {
                    var i:int = screen.records.getItemIndex(token.item);
                    screen.records.removeItemAt(i);
                    break;
                }

                case LogUpdateToken.UPDATE:
                {
                    screen.records.removeItemAt(token.index);
                    screen.records.addItemAt(token.item, token.index);
                    break;
                }
            }
        }

        private function screen_tweetHandler(event:RecordEvent):void
        {
            sendNotification(Config.RECORD_ADD, event.string);
        }

        private function changeRecordHandler(event:RecordEvent):void
        {
            sendNotification(Config.RECORD_UPDATE, event.record);
        }

        private function deleteRecordHandler(event:RecordEvent):void
        {
            sendNotification(Config.RECORD_DELETE, event.record);
        }

        private function loadRecordHandler(event:RecordEvent):void
        {
            sendNotification(Config.LOG_GET, event.string);
        }
    }
}
